home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Completions / Completions Source / Data / ConstData.h < prev    next >
Encoding:
Text File  |  1998-06-18  |  2.8 KB  |  87 lines  |  [TEXT/CWIE]

  1. // ConstData.h
  2.  
  3. #ifndef ConstData_h
  4. #define ConstData_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef Assert_h
  10. #include "Assert.h"
  11. #endif
  12.  
  13. class Data;
  14.  
  15. /*
  16.    A ConstData is a range of bytes, starting at Start() and
  17.    not including End().  You can't change them.
  18. */
  19.  
  20. class ConstData
  21.   {
  22.     private:
  23.         const uint8 *start;
  24.         const uint8 *end;
  25.     
  26.     public:
  27.         ConstData()        : start(0), end(0)    {}
  28.  
  29.         ConstData( const void *theStart, const void *theEnd )
  30.           : start( static_cast<const uint8 *>( theStart ) ),
  31.              end( static_cast<const uint8 *>( theEnd ) )
  32.           {
  33.             Assert( theEnd >= theStart );
  34.             Assert( theStart != 0 || theEnd == 0 );
  35.           }
  36.         
  37.         ConstData( const void *theStart, uint32 theLength )
  38.           : start( static_cast<const uint8 *>( theStart ) ),
  39.              end( static_cast<const uint8 *>( theStart ) + theLength )
  40.           {
  41.             Assert( theStart != 0 || theLength == 0 );
  42.           }
  43.  
  44.         const uint8 *Start() const                                { return start; }
  45.         const uint8 *End() const                                { return end; }
  46.  
  47.         uint32 Length() const                                    { return end - start; }
  48.                 
  49.         uint32 BoundedLength( uint32 bound ) const        { return ( Length() <= bound ) ? Length() : bound; }
  50.  
  51.         bool IsEmpty() const                                        { return start == end; }
  52.         bool Null() const                                            { return start == 0; }
  53.         
  54.         const uint8& operator[]( uint32 i ) const            { Assert( i < Length() ); return start[i]; }
  55.         
  56.         ConstData Head( uint32 position ) const            { Assert( position <= Length() ); return ConstData( start, start+position ); }
  57.         ConstData Tail( uint32 position ) const            { Assert( position <= Length() ); return ConstData( start+position, end ); }
  58.         ConstData Middle( uint32 s, uint32 e ) const        { Assert( s <= e );  Assert( e <= Length() ); return ConstData( start+s, start+e ); }
  59.         
  60.         void Truncate( uint32 position )                        { Assert( position <= Length() ); end = start + position; }
  61.         void LimitLength( uint32 limit )                        { if ( limit < Length() )  end = start + limit; }
  62.         
  63.         void Shorten( uint32 amount )                            { Assert( amount <= Length() ); start += amount; }
  64.         void Lengthen( uint32 amount )                        { end += amount; }
  65.         
  66.         bool StartsWith( ConstData ) const;
  67.         bool EndsWith( ConstData ) const;
  68.         bool Contains( ConstData ) const;
  69.         
  70.         bool Contains( const void *p ) const                { return start <= p && p < end; }
  71.         
  72.         ConstData operator>>( Data ) const;                    // returns the remainder, for chaining
  73.   };
  74.  
  75. bool operator==( const ConstData& a, const ConstData& b );
  76. bool operator<( const ConstData& a, const ConstData& b );
  77. bool operator<=( const ConstData& a, const ConstData& b );
  78.         
  79. inline bool operator!=( const ConstData& a, const ConstData& b )        { return !( a == b ); }
  80. inline bool operator>( const ConstData& a, const ConstData& b )        { return !( a <= b ); }
  81. inline bool operator>=( const ConstData& a, const ConstData& b )        { return !( a < b ); }
  82.  
  83. int32 Compare( ConstData a, ConstData b );
  84. int32 CompareReversed( ConstData a, ConstData b );
  85.  
  86. #endif
  87.